package kz.pro.client.form;

import kz.pro.shared.entity.UserEntity;
import kz.pro.shared.services.UserService;
import kz.pro.shared.services.UserServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UpdateForm extends DialogBox{
	private final UserServiceAsync userService = GWT
			.create(UserService.class);
	public UpdateForm(final UserEntity userEntity){
		
		setText("User form");
		final Label nameLabel = new Label("Name: ");
		final Label surnameLabel = new Label("Surname: ");
		final Label phoneNUmberLabel = new Label("Phone number: ");
		final Label phoneTypeLabel = new Label("Phone type: ");
		nameLabel.setWidth("60px");
		surnameLabel.setWidth("60px");
		phoneNUmberLabel.setWidth("60px");
		phoneTypeLabel.setWidth("60px");
		HorizontalPanel h1 = new HorizontalPanel();
		HorizontalPanel h2 = new HorizontalPanel();
		HorizontalPanel h3 = new HorizontalPanel();
		HorizontalPanel h4 = new HorizontalPanel();
		HorizontalPanel h5 = new HorizontalPanel();
		HorizontalPanel h6 = new HorizontalPanel();
		h5.setWidth("70px");
		h6.setWidth("60px");
		final TextBox name = new TextBox();
		name.setText(userEntity.getName());
		final TextBox surname = new TextBox();
		surname.setText(userEntity.getSurname());
		final TextBox phoneNumber = new TextBox();
		phoneNumber.setText(userEntity.getPhoneNumber());
		final TextBox phoneType = new TextBox();
		phoneType.setText(userEntity.getPhoneType());
		h1.add(nameLabel);
		h1.add(name);
		h2.add(surnameLabel);
		h2.add(surname);
		h3.add(phoneNUmberLabel);
		h3.add(phoneNumber);
		h4.add(phoneTypeLabel);
		h4.add(phoneType);
		final Button save = new Button("update");
		final Button close = new Button("close");
		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		final VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.add(h1);
		verticalPanel.add(h2);
		verticalPanel.add(h3);
		verticalPanel.add(h4);
		horizontalPanel.add(h6);
		horizontalPanel.add(save);
		horizontalPanel.add(h5);
		horizontalPanel.add(close);
		verticalPanel.add(horizontalPanel);
		
		
		save.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(name.getText()!=null && surname.getText()!=null && phoneNumber.getText()!=null && phoneType.getText()!=null){
					userService.updateUser(userEntity.getUserId(), name.getText(), surname.getText(), phoneNumber.getText(), phoneType.getText(), new AsyncCallback<Void>() {
	
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Error occured");
							
						}
	
						@Override
						public void onSuccess(Void result) {
							Window.Location.reload();
							hide();
							
						}
					});
				}
			}
		});
		
		
		close.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				hide();
				
			}
		});
		setWidget(verticalPanel);
	}
		
}
